home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / LOAD.C < prev    next >
C/C++ Source or Header  |  1992-01-15  |  10KB  |  320 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/src/microcode/RCS/load.c,v 9.31 1992/01/16 01:47:56 jinx Exp $
  4.  
  5. Copyright (c) 1987-92 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* This file contains common code for reading internal
  36.    format binary files. */
  37.  
  38. #include "fasl.h"
  39.  
  40. #define FASL_FILE_FINE            0
  41. #define FASL_FILE_TOO_SHORT        1
  42. #define FASL_FILE_NOT_FASL        2
  43. #define FASL_FILE_BAD_MACHINE        3
  44. #define FASL_FILE_BAD_VERSION        4
  45. #define FASL_FILE_BAD_SUBVERSION    5
  46. #define FASL_FILE_BAD_PROCESSOR        6
  47. #define FASL_FILE_BAD_INTERFACE        7
  48.  
  49. #ifndef BYTE_INVERSION
  50.  
  51. #define NORMALIZE_HEADER(header, size, base, count)
  52. #define NORMALIZE_REGION(region, size)
  53.  
  54. #else /* BYTE_INVERSION */
  55.  
  56. void
  57.   EXFUN (Byte_Invert_Region, (long *, long)),
  58.   EXFUN (Byte_Invert_Header, (long *, long, long, long));
  59.  
  60. #define NORMALIZE_HEADER Byte_Invert_Header
  61. #define NORMALIZE_REGION Byte_Invert_Region
  62.  
  63. #endif /* BYTE_INVERSION */
  64.  
  65. /* Static storage for some shared variables */
  66.  
  67. static Boolean
  68.   band_p;
  69.  
  70. static long
  71.   Machine_Type, Version, Sub_Version,
  72.   Dumped_Object, Dumped_Stack_Top,
  73.   Heap_Base, Heap_Count,
  74.   Const_Base, Const_Count,
  75.   Dumped_Heap_Top, Dumped_Constant_Top,
  76.   Primitive_Table_Size, Primitive_Table_Length,
  77.   dumped_processor_type, dumped_interface_version;
  78.  
  79. static unsigned long
  80.   dumped_checksum, computed_checksum;
  81.  
  82. static SCHEME_OBJECT
  83.   Ext_Prim_Vector,
  84.   dumped_utilities;
  85.  
  86. void
  87. DEFUN_VOID (print_fasl_information)
  88. {
  89.   printf ("FASL File Information:\n\n");
  90.   printf ("Machine = %ld; Version = %ld; Subversion = %ld\n",
  91.       Machine_Type, Version, Sub_Version);
  92.   if ((dumped_processor_type != 0) || (dumped_interface_version != 0))
  93.   {
  94.     printf ("Compiled code interface version = %ld; Processor type = %ld\n",
  95.         dumped_interface_version, dumped_processor_type);
  96.   }
  97.   if (band_p)
  98.   {
  99.     printf ("The file contains a dumped image (band).\n");
  100.   }
  101.  
  102.   printf ("\nRelocation Information:\n\n");
  103.   printf ("Heap Count = %ld; Heap Base = 0x%lx; Heap Top = 0x%lx\n",
  104.       Heap_Count, Heap_Base, Dumped_Heap_Top);
  105.   printf ("Const Count = %ld; Const Base = 0x%lx; Const Top = 0x%lx\n",
  106.       Const_Count, Const_Base, Dumped_Constant_Top);
  107.   printf ("Stack Top = 0x%lx\n", Dumped_Stack_Top);
  108.  
  109.   printf ("\nDumped Objects:\n\n");
  110.   printf ("Dumped object at 0x%lx (as read from file)\n", Dumped_Object);
  111.   printf ("Compiled code utilities vector = 0x%lx\n", dumped_utilities);
  112.   if (Ext_Prim_Vector != SHARP_F)
  113.   {
  114.     printf ("External primitives vector = 0x%lx\n", Ext_Prim_Vector);
  115.   }
  116.   else
  117.   {
  118.     printf ("Length of primitive table = %ld\n", Primitive_Table_Length);
  119.   }
  120.   printf ("Checksum = 0x%lx\n", dumped_checksum);
  121.   return;
  122. }
  123.  
  124. long
  125. DEFUN (initialize_variables_from_fasl_header, (buffer),
  126.        SCHEME_OBJECT * buffer)
  127. {
  128.   SCHEME_OBJECT Pointer_Heap_Base, Pointer_Const_Base;
  129.  
  130.   if (buffer[FASL_Offset_Marker] != FASL_FILE_MARKER)
  131.   {
  132.     return (FASL_FILE_NOT_FASL);
  133.   }
  134.   NORMALIZE_HEADER (buffer,
  135.             (sizeof(buffer) / sizeof(SCHEME_OBJECT)),
  136.             buffer[FASL_Offset_Heap_Base],
  137.             buffer[FASL_Offset_Heap_Count]);
  138.   Heap_Count = OBJECT_DATUM (buffer[FASL_Offset_Heap_Count]);
  139.   Pointer_Heap_Base = buffer[FASL_Offset_Heap_Base];
  140.   Heap_Base = OBJECT_DATUM (Pointer_Heap_Base);
  141.   Dumped_Object = OBJECT_DATUM (buffer[FASL_Offset_Dumped_Obj]);
  142.   Const_Count = OBJECT_DATUM (buffer[FASL_Offset_Const_Count]);
  143.   Pointer_Const_Base = buffer[FASL_Offset_Const_Base];
  144.   Const_Base = OBJECT_DATUM (Pointer_Const_Base);
  145.   Version = The_Version(buffer[FASL_Offset_Version]);
  146.   Sub_Version = The_Sub_Version(buffer[FASL_Offset_Version]);
  147.   Machine_Type = The_Machine_Type(buffer[FASL_Offset_Version]);
  148.   Dumped_Stack_Top = OBJECT_DATUM (buffer[FASL_Offset_Stack_Top]);
  149.   Dumped_Heap_Top =
  150.     ADDRESS_TO_DATUM (MEMORY_LOC (Pointer_Heap_Base, Heap_Count));
  151.   Dumped_Constant_Top =
  152.     ADDRESS_TO_DATUM (MEMORY_LOC (Pointer_Const_Base, Const_Count));
  153.  
  154.   if (Sub_Version < FASL_MERGED_PRIMITIVES)
  155.   {
  156.     Primitive_Table_Length = 0;
  157.     Primitive_Table_Size = 0;
  158.     Ext_Prim_Vector =
  159.       (OBJECT_NEW_TYPE (TC_CELL, (buffer [FASL_Offset_Ext_Loc])));
  160.   }
  161.   else
  162.   {
  163.     Primitive_Table_Length = OBJECT_DATUM (buffer[FASL_Offset_Prim_Length]);
  164.     Primitive_Table_Size = OBJECT_DATUM (buffer[FASL_Offset_Prim_Size]);
  165.     Ext_Prim_Vector = SHARP_F;
  166.   }
  167.  
  168.   if (Sub_Version < FASL_INTERFACE_VERSION)
  169.   {
  170.     /* This may be all wrong, but... */
  171.     band_p = false;
  172.     dumped_processor_type = 0;
  173.     dumped_interface_version = 0;
  174.     dumped_utilities = SHARP_F;
  175.   }
  176.   else
  177.   {
  178.     SCHEME_OBJECT temp;
  179.  
  180.     temp = buffer[FASL_Offset_Ci_Version];
  181.  
  182.     band_p = CI_BAND_P(temp);
  183.     dumped_processor_type = CI_PROCESSOR(temp);
  184.     dumped_interface_version = CI_VERSION(temp);
  185.     dumped_utilities = buffer[FASL_Offset_Ut_Base];
  186.   }
  187.  
  188. #ifndef INHIBIT_FASL_VERSION_CHECK
  189.   /* The error messages here should be handled by the runtime system! */
  190.  
  191.   if ((Version != FASL_READ_VERSION) ||
  192. #ifndef BYTE_INVERSION
  193.       (Machine_Type != FASL_INTERNAL_FORMAT) ||
  194. #endif
  195.       (Sub_Version < FASL_READ_SUBVERSION) ||
  196.       (Sub_Version > FASL_SUBVERSION))
  197.   {
  198.     fprintf(stderr, "\nread_file:\n");
  199.     fprintf(stderr,
  200.         "FASL File: Version %4d Subversion %4d Machine Type %4d.\n",
  201.         Version, Sub_Version , Machine_Type);
  202.     fprintf(stderr,
  203.         "Expected:  Version %4d Subversion %4d Machine Type %4d.\n",
  204.         FASL_READ_VERSION, FASL_READ_SUBVERSION, FASL_INTERNAL_FORMAT);
  205.  
  206.     return ((Machine_Type != FASL_INTERNAL_FORMAT)    ?
  207.         FASL_FILE_BAD_MACHINE            :
  208.         ((Version != FASL_READ_VERSION)        ?
  209.          FASL_FILE_BAD_VERSION            :
  210.          FASL_FILE_BAD_SUBVERSION));
  211.   }
  212.  
  213. #endif /* INHIBIT_FASL_VERSION_CHECK */
  214.  
  215. #ifndef INHIBIT_COMPILED_VERSION_CHECK
  216.  
  217.   /* Is the compiled code "loadable" here? */
  218.  
  219.   {
  220.     extern long compiler_processor_type, compiler_interface_version;
  221.  
  222.     if (((dumped_processor_type != 0) &&
  223.     (dumped_processor_type != compiler_processor_type)) ||
  224.     ((dumped_interface_version != 0) &&
  225.      (dumped_interface_version != compiler_interface_version)))
  226.     {
  227.       fprintf (stderr, "\nread_file:\n");
  228.       fprintf (stderr,
  229.            "FASL File: compiled code interface %4d; processor %4d.\n",
  230.            dumped_interface_version, dumped_processor_type);
  231.       fprintf (stderr,
  232.            "Expected:  compiled code interface %4d; processor %4d.\n",
  233.            compiler_interface_version, compiler_processor_type);
  234.       return (((dumped_processor_type != 0) &&
  235.            (dumped_processor_type != compiler_processor_type))    ?
  236.           FASL_FILE_BAD_PROCESSOR                    :
  237.           FASL_FILE_BAD_INTERFACE);
  238.     }
  239.   }
  240.  
  241. #endif /* INHIBIT_COMPILED_VERSION_CHECK */
  242.  
  243.   dumped_checksum = (buffer [FASL_Offset_Check_Sum]);
  244.  
  245. #ifndef INHIBIT_CHECKSUMS
  246.  
  247.   {
  248.     extern unsigned long
  249.       EXFUN (checksum_area, (unsigned long *, long, unsigned long));
  250.  
  251.     computed_checksum =
  252.       (checksum_area (((unsigned long *) &buffer[0]),
  253.               ((long) (FASL_HEADER_LENGTH)),
  254.               ((unsigned long) 0)));
  255.  
  256.   }
  257.  
  258. #endif /* INHIBIT_CHECKSUMS */
  259.  
  260.   return (FASL_FILE_FINE);
  261. }
  262.  
  263. long
  264. DEFUN_VOID (Read_Header)
  265. {
  266.   SCHEME_OBJECT header[FASL_HEADER_LENGTH];
  267.  
  268.   if ((Load_Data (FASL_HEADER_LENGTH, header)) !=
  269.       FASL_HEADER_LENGTH)
  270.   {
  271.     return (FASL_FILE_TOO_SHORT);
  272.   }
  273.   return (initialize_variables_from_fasl_header (&header[0]));
  274. }
  275.  
  276. #ifdef BYTE_INVERSION
  277.  
  278. static Boolean Byte_Invert_Fasl_Files;
  279.  
  280. void
  281. DEFUN (Byte_Invert_Header, (Header, Headsize, Test1, Test2),
  282.        long * Header
  283.        AND long Headsize
  284.        AND long Test1
  285.        AND long Test2)
  286. {
  287.   Byte_Invert_Fasl_Files = false;
  288.  
  289.   if ((Test1 & 0xff) == TC_BROKEN_HEART &&
  290.       (Test2 & 0xff) == TC_BROKEN_HEART &&
  291.       (OBJECT_TYPE (Test1) != TC_BROKEN_HEART ||
  292.        OBJECT_TYPE (Test2) != TC_BROKEN_HEART))
  293.   {
  294.     Byte_Invert_Fasl_Files = true;
  295.     Byte_Invert_Region(Header, Headsize);
  296.   }
  297.   return;
  298. }
  299.  
  300. void
  301. DEFUN (Byte_Invert_Region, (Region, Size),
  302.        long * Region
  303.        AND long Size)
  304. {
  305.   register long word, size;
  306.  
  307.   if (Byte_Invert_Fasl_Files)
  308.   {
  309.     for (size = Size; size > 0; size--, Region++)
  310.     {
  311.       word = (*Region);
  312.       *Region = (((word>>24)&0xff) | ((word>>8)&0xff00) |
  313.          ((word<<8)&0xff0000) | ((word<<24)&0xff000000));
  314.     }
  315.   }
  316.   return;
  317. }
  318.  
  319. #endif /* BYTE_INVERSION */
  320.